package cn.djb.qz2021;

import java.io.*;
import java.util.HashMap;

public class Main_123 {
    public static void main(String[] args) throws IOException {
        BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        int n = Integer.parseInt(read.readLine().trim());
        char[] cs = read.readLine().trim().toCharArray();

        System.out.println(new Solution().maxSum1("010101")); // 7
        System.out.println(new Solution().maxSum1("11111000111011101100")); // 94
        System.out.println(new Solution().maxSum1("1100")); // 6


    }

    private static class Solution {
        public int maxSum1(String s) {
            int answer = 0;
            // i,0/1,index/sum/same;
            int[][][] sum = new int[s.length()][2][3];

            if (s.charAt(0) == '0') {
                sum[0][0] = new int[]{1, 1, 0};
                sum[0][1] = new int[]{0, 0, 0};
            } else {
                sum[0][0] = new int[]{0, 0, 0};
                sum[0][1] = new int[]{1, 1, 0};
            }
            answer = max(answer, sum[0][0][1], sum[0][1][1]);
            for (int i = 1; i < s.length(); i++) {
                if (s.charAt(i) == '0') {
                    sum[i][0][2] = 0;
                    sum[i][0][0] = sum[i - 1][0][0] + 1;
                    sum[i][0][1] = sum[i - 1][0][1] + sum[i][0][0];

                    sum[i][1][2] = sum[i - 1][1][2] + 1;
                    sum[i][1][0] = sum[i - 1][1][0];
                    sum[i][1][1] = sum[i - 1][1][1];
                    answer = max(answer, sum[i][0][1], sum[i - 1][1][1] + factorial(sum[i][1][2]));
                } else {
                    sum[i][0][2] = sum[i - 1][0][2] + 1;
                    sum[i][0][0] = sum[i - 1][0][0];
                    sum[i][0][1] = sum[i - 1][0][1];

                    sum[i][1][2] = 0;
                    sum[i][1][0] = sum[i - 1][1][0] + 1;
                    sum[i][1][1] = sum[i - 1][1][1] + sum[i][1][0];
                    answer = max(answer, sum[i - 1][0][1] + factorial(sum[i][0][2]), sum[i][1][1]);
                }
            }
            return answer;
        }

        int max(int a, int b, int c) {
            if (a < b) {
                a = b;
            }
            if (a < c) {
                a = c;
            }
            return a;
        }

        final HashMap<Integer, Integer> map = new HashMap<>();

        int factorial(int n) {
            if (map.containsKey(n)) {
                return map.get(n);
            }
            if (n == 1 || n == 0) {
                return n;
            }
            final int sum = factorial(n - 1) + n;
            map.put(n, sum);
            return sum;
        }
    }
}